<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class M_home extends CI_Model {

	public function __construct() {
		parent::__construct();
	}

	function get_pengemudi() {
		$sql = "SELECT * FROM pengemudi where status = 'Aktif' order by tgl_daftar desc limit 5";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_penumpang() {
		$sql = "SELECT * FROM penumpang where status = 'Aktif' order by tgl_daftar desc limit 5";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_pengalaman() {
		$sql = "SELECT * FROM pengalaman order by id_pengalaman desc limit 5";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_konten($params) {
		$sql = "SELECT * FROM konten where slug = ?";
		$query = $this->db->query($sql,$params);
		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_konten_id($params) {
		$sql = "SELECT * FROM konten where id_konten = ?";
		$query = $this->db->query($sql,$params);
		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_kota() {
		$sql = "SELECT * FROM kota order by nama_kota";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_jenis_kendaraan() {
		$sql = "SELECT * FROM jenis_kendaraan";
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function insert_kendaraan($params_kendaraan){
		$insert = $this->db->insert('kendaraan',$params_kendaraan);
		return $insert;
	}

	function insert_pengemudi($params_pengemudi){
		$insert = $this->db->insert('pengemudi',$params_pengemudi);
		return $insert;
	}

	function insert_penumpang($params_penumpang){
		$insert = $this->db->insert('penumpang',$params_penumpang);
		return $insert;
	}

	function login_pengemudi($username,$password){
		$sql="SELECT * FROM pengemudi WHERE id_driver='".$username."' AND password='".$password."' LIMIT 1";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return false;
		}	
	}

	function login_penumpang($username,$password){
		$sql="SELECT * FROM penumpang WHERE id_penumpang = '".$username."' AND password='".$password."' LIMIT 1";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return false;
		}	
	}

	function cekStatusPengemudi($id){
		$sql="SELECT * FROM pengemudi WHERE id_driver=$id";
		//echo $sql;
		$query=$this->db->query($sql);
		
		if ($query->num_rows()>0){
			$tmp_data=$query->row_array();
			if($tmp_data['status']=='Aktif'){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}	
	}

	function cekStatusPenumpang($id){
		$sql="SELECT * FROM penumpang WHERE id_penumpang= '".$id."'";
		//echo $sql;
		$query=$this->db->query($sql);
		
		if ($query->num_rows()>0){
			$tmp_data=$query->row_array();
			if($tmp_data['status']=='Aktif'){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}	
	}

	function get_tumpangan($params) {
		$sql = "SELECT * FROM tumpangan t inner join pengemudi p on t.id_driver = p.id_driver where id_penumpang = ?";
		$query = $this->db->query($sql,$params);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}
}